namespace Romer
{
    using System;
    using System.IO;

    internal class ROMEditor
    {
        private byte[] BoundArray;
        public int bytepointer;
        public string NdsFile;
        public byte[] Philsbyte = new byte[0x840];

        public void AppendIcon(bool check, int Addtostart)
        {
            this.GetBounds();
            int index = 0;
            FileStream output = new FileStream(this.NdsFile, FileMode.Open);
            BinaryWriter writer = new BinaryWriter(output);
            if (check)
            {
                writer.BaseStream.Position = this.bytepointer - Addtostart;
            }
            else
            {
                writer.BaseStream.Position = BitConverter.ToInt32(this.BoundArray, 0);
                Addtostart = 0;
            }
            byte[] buffer = new byte[0x840 + Addtostart];
            index = 0;
            while (index < Addtostart)
            {
                buffer[index] = 0;
                index++;
            }
            for (int i = 0; i < this.Philsbyte.Length; i++)
            {
                buffer[index] = this.Philsbyte[i];
                index++;
            }
            writer.Write(buffer);
            output.Close();
            Console.WriteLine("BREAK: ROM EDITED");
            this.BoundArray = null;
        }

        public int[] FileWritePoint()
        {
            FileStream stream = new FileStream(this.NdsFile, FileMode.Open);
            int num = Convert.ToInt32(stream.Length);
            int num2 = num % 0x200;
            stream.Close();
            int[] numArray = new int[2];
            num2 = 0x200 - num2;
            numArray[0] = num;
            numArray[1] = num2;
            this.bytepointer = num + num2;
            return numArray;
        }

        public void GetBounds()
        {
            FileStream input = new FileStream(this.NdsFile, FileMode.Open);
            this.BoundArray = new BinaryReader(input) { BaseStream = { Position = 0x68L } }.ReadBytes(4);
            input.Close();
            Console.WriteLine("BREAK: GETBOUNDS");
        }

        public void WriteBannerStart()
        {
            FileStream output = new FileStream(this.NdsFile, FileMode.Open);
            new BinaryWriter(output) { BaseStream = { Position = 0x68L } }.Write(this.bytepointer);
            output.Close();
            Console.WriteLine("BREAK: WRITE HEADER POINTER");
        }
    }
}

